package net.media.datafw.transport.mvc;

import java.beans.PropertyEditorSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.media.datafw.transport.TransportException;

public class TransportableEditor extends PropertyEditorSupport {
	Logger logger = LoggerFactory.getLogger(getClass());

	private TransportType transportType = null;

	public TransportableEditor(TransportType transportType) {
		this.transportType = transportType;
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		try {
			setValue(((this.transportType).getTransporter()).deserialize(text.getBytes()));
		} catch (TransportException e) {
			logger.error("@ TransportableEditor setAsText", e);
			throw new IllegalArgumentException(e);
		}
	}

	@Override
	public String getAsText() throws IllegalArgumentException {
		String toRet = null;
		try {
			toRet = ((this.transportType).getTransporter()).serialize(getValue()).toString();
		} catch (TransportException e) {
			logger.error("@ TransportableEditor getAsText", e);
			throw new IllegalArgumentException(e);
		}
		return toRet;
	}
}
